import base64
import io
import secrets
import string
from PIL import Image, ImageDraw, ImageFont


def gen_random_str(count: int) -> str:
    characters = string.ascii_letters + string.digits
    random_string = ''.join(secrets.choice(characters) for _ in range(count))
    return random_string

def gen_captcha_img_base64(captcha_str: str) -> str:
    image = Image.new("RGB", (125, 50), "white")
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", 36)
    draw.text((10, 10), captcha_str, font=font, fill="black")

    buffered = io.BytesIO()
    image.save(buffered, format="PNG")
    image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
    return image_base64

